﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第五次作业
{
    public partial class Form1 : Form
    {
        private HashSet<string> crawledUrls = new HashSet<string>();
        private List<string> phoneNumbers = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private async void SearchButton_Click(object sender, EventArgs e)
        {
            string searchKeyword = SearchTextBox.Text;
            if (string.IsNullOrWhiteSpace(searchKeyword))
            {
                MessageBox.Show("请输入搜索关键词");
                return;
            }
            //使用必应搜索
            string bingSearchUrl = $"https://www.bing.com/search?q={searchKeyword}";
            //无需堵塞，异步进行搜索
            await CrawlAsync(bingSearchUrl, 100);
            DisplayCrawledUrls();
            DisplayPhoneNumbers();
        }
        private async Task CrawlAsync(string url, int maxPhoneNumbers)
        {
            if (crawledUrls.Contains(url))
                return;
            //将输入URL加入爬虫网址中
            crawledUrls.Add(url);

            using (var httpClient = new HttpClient())
            {
                try
                {
                    string pageContent = await httpClient.GetStringAsync(url);
                    FindPhoneNumbers(pageContent, maxPhoneNumbers);
                    //递归爬取第一个网页中的链接，直到爬到所需的数量
                    if (phoneNumbers.Count < maxPhoneNumbers)
                    {
                        
                        List<string> links = ExtractLinks(pageContent);
                        foreach (string link in links)
                        {
                            //递归调用爬虫
                            await CrawlAsync(link, maxPhoneNumbers);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Handle errors here
                    MessageBox.Show($"Error while crawling {url}: {ex.Message}");
                }
            }
        }
        private List<string> ExtractLinks(string pageContent)
        {
            List<string> links = new List<string>();
            MatchCollection linkMatches = Regex.Matches(pageContent, @"<a\s+href=""(http[^""]+)""");
            foreach (Match match in linkMatches)
            {
                string link = match.Groups[1].Value;
                links.Add(link);
            }
            return links;
        }
        private void FindPhoneNumbers(string text, int maxPhoneNumbers)
        {
            //返回text中满足电话号码格式的字符串并保存
            MatchCollection phoneMatches = Regex.Matches(text, @"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b");
            foreach (Match match in phoneMatches)
            {
                string phoneNumber = match.Value;
                if (!phoneNumbers.Contains(phoneNumber))
                {
                    phoneNumbers.Add(phoneNumber);
                    if (phoneNumbers.Count >= maxPhoneNumbers)
                        break;
                }
            }
        }
        private void DisplayCrawledUrls()
        {
            CrawledUrlsListBox.Items.Clear();
            foreach (string url in crawledUrls)
            {
                CrawledUrlsListBox.Items.Add(url);
            }
        }

        private void DisplayPhoneNumbers()
        {
            PhoneNumbersListBox.Items.Clear();
            foreach (string phoneNumber in phoneNumbers)
            {
                PhoneNumbersListBox.Items.Add(phoneNumber);
            }
        }
    }
}
